//
//  CZVisitorView.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class CZVisitorView: UIView {
    
    // MARK: ------懒加载控件pod update --verbose --no-repo-update------
    
    var callBackClosure:(() -> ())?
    
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var makeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel: UILabel = {
    let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
         label.text = "我了个去啊,过补了了啊,都是坑啊"
        label.font = UIFont.systemFontOfSize(14)
    label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
         button.addTarget(self, action: #selector(CZVisitorView.regist), forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        
        return button
    }()
    

    private lazy var loginBUtton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: #selector(CZVisitorView.login), forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
         button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()

    }
    


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
    backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImageView)
        addSubview(makeImageView)
        addSubview(registerButton)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginBUtton)
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        makeImageView.snp_makeConstraints{ (make) -> Void in
            make.center.equalTo(cycleImageView)
    }
    
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSize(width: 224, height: 40))
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginBUtton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        
    }
        // MARK: ------点击事件------
        @ objc private func login() {
            
            
            callBackClosure?()
        }
        
        @ objc private  func regist() {
            
            callBackClosure?()
            
        }
    
    //动画
        private func startAnim(){
            let animation = CABasicAnimation(keyPath: "transform.rotation")
            
            animation.toValue = 2 * M_PI
            
            animation.duration = 20
            animation.repeatCount = MAXFLOAT
            //防止动画释放
            animation.removedOnCompletion = false
            cycleImageView.layer.addAnimation(animation, forKey: nil)
        }
        
        

        
        
        //修改访客视图信息
        func setVistorInfo(title: String?, imageName: String?){
//        messageLabel.text = title
//            cycleImageView.image = UIImage(named: imageName)
        
            if let t = title, imageNam = imageName{
            
                messageLabel.text = t
                cycleImageView.image = UIImage(named: imageNam)
            
            cycleImageView.hidden = true
            }else
            {
            cycleImageView.hidden = false
                startAnim()
            
            
            }
        }
   
    
        
        
        //设置约束
        //使用代码的形式自动布局
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

